package cn.itcast.server.service.handler;

import cn.itcast.server.session.SessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

@ChannelHandler.Sharable
@Slf4j
public class QuitRemindHandler extends ChannelInboundHandlerAdapter {
    // 用户通道请求正常关闭
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        //解绑
        SessionFactory.getSession().unbind(ctx.channel());
        log.error("通道 {} 已关闭！", ctx.channel());
    }

    // 用户通道异常关闭
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //解绑
        SessionFactory.getSession().unbind(ctx.channel());
        log.error("通道 {} 异常关闭，异常：{}", ctx.channel(), cause.getMessage());
    }
}
